cmake_minimum_required(VERSION 3.21)
project(canpp)

set(CMAKE_CXX_STANDARD 20)

option(BUILD_CANPP_SHARED_LIBS "Build shared libraries of canpp" ON)
option(COMPILE_CANPP_EXAMPLE "Build example of canpp" ON)
option(CANPP_DISABLE_LOG "Disable Log function completely for canpp" OFF)

set(PROJECT_ROOT_DIR ${CMAKE_SOURCE_DIR})
set(CANPP_LOG_DIR ${PROJECT_ROOT_DIR}/log/canpp)
message(STATUS "canpp log path: ${CANPP_LOG_DIR}")

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

file(GLOB SRC_FILES src/*.cpp src/*.h src/private/*.cpp src/private/*.h src/private/*.hpp)
if (BUILD_CANPP_SHARED_LIBS)
    add_library(canpp SHARED ${SRC_FILES})
else ()
    add_library(canpp STATIC ${SRC_FILES})
endif ()
if (NOT CANPP_DISABLE_LOG)
    if (BUILD_CANPP_SHARED_LIBS)
        target_include_directories(canpp PUBLIC 3rdparty/BqLog/dist/dynamic_lib/include)
    else ()
        target_include_directories(canpp PUBLIC 3rdparty/BqLog/dist/static_lib/include)
    endif ()
endif ()

target_include_directories(canpp PRIVATE 3rdparty/bs_thread_pool/include)
target_include_directories(canpp PRIVATE 3rdparty/Ring-Buffer)

target_include_directories(canpp PRIVATE src)

if (CANPP_DISABLE_LOG)
    target_compile_definitions(canpp PRIVATE CANPP_DISABLE_LOG=1)
endif ()

target_compile_definitions(canpp PRIVATE CANPP_LOG_DIR="${CANPP_LOG_DIR}")
target_compile_definitions(canpp PRIVATE CAN_REAPER_MAX_EVENT=100)

if (NOT CANPP_DISABLE_LOG)
    if (BUILD_CANPP_SHARED_LIBS)
        target_link_libraries(canpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/BqLog/dist/dynamic_lib/linux64/${CMAKE_BUILD_TYPE}/libBqLog.so)
    else ()
        target_link_libraries(canpp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/BqLog/dist/static_lib/linux64/${CMAKE_BUILD_TYPE}/libBqLog.so)
    endif ()
endif ()

if (COMPILE_CANPP_EXAMPLE)
    add_executable(example_SenderReader example/reader&sender.cpp)
    add_executable(example_CanDriver example/canDriver.cpp)
    target_include_directories(example_CanDriver PRIVATE src)
    target_include_directories(example_CanDriver PRIVATE src)
    target_include_directories(example_SenderReader PRIVATE 3rdparty/Ring-Buffer)
    target_link_libraries(example_SenderReader canpp)
    target_link_libraries(example_CanDriver canpp)
endif ()